package com.collabscm.pf.web.taglib;


import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.collabscm.pf.exception.UncheckedException;

/**
 * 分页标签类
 * @author tony
 */
public class PageTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	/**
	 * 设置位置:左右中
	 */
	private String align;
	/**
	 * 分页数
	 */
	private int pageCount = 0;
	/**
	 * 当前选中页
	 */
	private int selectPage = 0;
	/**
	 * 宽度
	 */
	private String width;
	/**
	 * 样式
	 */
	private String cs;
	/**
	 * 链接地址
	 */
	private String url;
	public final static String the = "第";
	public final static String page = "页";
	public final static String all = "共";
	public final static String firstPage = "首页";
	public final static String lastPage = "末页";
	public final static String previousPage = "上一页";
	public final static String nextpage = "下一页";
	
	public int doStartTag() throws JspException {
		String head = "<table align='" + getAlign()
				+ "' cellpadding=\"0\" cellspacing=\"0\" width='" + getWidth()
				+ "' class='" + getCs() + "'><tr align=\"right\"><td>";
		String context = "";
		if (getPageCount() == 0)
			context = the + getPageCount() + page + "/" + all + getPageCount()
					+ page + "&nbsp;";
		else
			context = the + getSelectPage() + page + "/" + all + getPageCount()
					+ page + "&nbsp;";
		// link分页显示
		int prePage = getSelectPage() - 1;
		int nextPage = getSelectPage() + 1;
		if (getSelectPage() == 0 || getSelectPage() == 1) {
			context += "&nbsp;" + firstPage + "&nbsp;";
			context += "&nbsp;" + previousPage + "&nbsp;";
		} else {
			context += "<a href='" + getUrl() + "1'>&nbsp;" + firstPage
					+ "&nbsp;</a>";
			context += "<a href='" + getUrl() + prePage + "'>&nbsp;"
					+ previousPage + "&nbsp;</a>";
		}
		if (getSelectPage() == getPageCount() || getPageCount() == 0) {
			context += "&nbsp;" + nextpage + "&nbsp;";
			context += "&nbsp;" + lastPage + "&nbsp;";
		} else {
			context += "<a href='" + getUrl() + nextPage + "'>&nbsp;"
					+ nextpage + "&nbsp;</a>";
			context += "<a href='" + getUrl() + getPageCount() + "'>&nbsp;"
					+ lastPage + "&nbsp;</a>";
		}
		// selectBox分页显示
		context += "&nbsp;Go<SELECT NAME=\"selectPage\"  onchange=\"selectPages(this.value)\">";
		if (getPageCount() == 0) {
			context += "<option  value=\"0\"></option>";
		} else {
			for (int i = 1; i <= getPageCount(); i++) {
				if (i == getSelectPage()) {
					context += "<option  value='" + i + "' selected>" + i
							+ "</option>";
				} else {
					context += "<option  value='" + i + "'>" + i + "</option>";
				}
			}
		}
		context += "</SELECT>";
		String js = "<script language=\"JavaScript\">function selectPages(i){window.location.href='"
				+ getUrl() + "'+i;}</script>";
		JspWriter out = (JspWriter) pageContext.getOut();
		try {
			out.println(head + context + "</td></tr></table>" + js);
		} catch (IOException e) {
			throw new UncheckedException("标签生成错误", e);
		}
		return EVAL_PAGE;
	} 
	
	/**
	 * @return 返回 cs。
	 */
	public String getCs() {
		return cs;
	}
	/**
	 * @param cs 要设置的 cs。
	 */
	public void setCs(String cs) {
		this.cs = cs;
	}
	
	/**
	 * @return 返回 pageCount。
	 */
	public int getPageCount() {
		return pageCount;
	}
	/**
	 * @param pageCount 要设置的 pageCount。
	 */
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	/**
	 * @return 返回 width。
	 */
	public String getWidth() {
		return width;
	}
	/**
	 * @param width 要设置的 width。
	 */
	public void setWidth(String width) {
		this.width = width;
	}
	/**
	 * @return 返回 url。
	 */
	public String getUrl() {
		return url;
	}
	/**
	 * @param url 要设置的 url。
	 */
	public void setUrl(String url) {
		this.url = url;
	}
	/**
	 * @return 返回 align。
	 */
	public String getAlign() {
		return align;
	}
	/**
	 * @param align 要设置的 align。
	 */
	public void setAlign(String align) {
		this.align = align;
	}
	/**
	 * @return 返回 selectPage。
	 */
	public int getSelectPage() {
		return selectPage;
	}
	/**
	 * @param selectPage 要设置的 selectPage。
	 */
	public void setSelectPage(int selectPage) {
		this.selectPage = selectPage;
	}
}
